<template>
  <div :class="{'hidden':hidden}" class="pagination-container">
    <el-select
      v-if="undefined !== reorderOptionList && reorderOptionList.length > 0"
      v-model="reorderBy"
      style="display: block;width:130px"
      size="mini"
      clearable
      placeholder="默认排序"
      value=""
      @change="changeReorder"
    >
      <el-option
        v-for="(item, index) in reorderOptionList"
        :key="index"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <i class="el-icon-refresh refresh-button" @click="handleRefreshClick" />
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scrollTo'

export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array,
      default() {
        return [5, 10, 20, 30, 50, 100, 200, 500, 1000, 2000]
      }
    },
    orderBy: {
      type: String,
      default: ''
    },
    reorderOptionList: {
      type: Array,
      default() {
        return []
      }
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    reorderBy: {
      get() {
        return this.orderBy
      },
      set(val) {
        this.$emit('update:orderBy', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    changeReorder(val) {
      this.$emit('pagination', { page: this.currentPage, orderBy: val, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleSizeChange(val) {
      this.$emit('pagination', { page: this.currentPage, limit: val, orderBy: this.orderBy })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      // 解决数据量过多，拉到最底部，点击分页滚动条回到顶部
      if (document.getElementsByClassName('el-dialog__wrapper').length) {
        document.getElementsByClassName('el-dialog__wrapper')[0].scrollTo(0, 0)
      }
      this.$emit('pagination', { page: val, limit: this.pageSize, orderBy: this.orderBy })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleRefreshClick(e) {
      this.$emit('pagination', { page: this.page, limit: this.pageSize, orderBy: this.orderBy })
    }
  }
}
</script>
<style lang="scss">
  .pagination-container {
    padding-top: 16px;
    display: flex;
  }

  .pagination-container.hidden {
    display: none;
  }

  .refresh-button {
    float: left;
    width: 50px;
    text-align: center;
    height: 32px;
    line-height: 32px;
    padding: 0px;
    margin: 0;
  }

  .refresh-button:hover {
    opacity: 0.8;
  }

  .refresh-button:active {
    color: #409EFF;
  }
</style>
